package javablackbelt.propertyeditors;

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class SimpleDateEditor extends PropertyEditorSupport {

    private final static String DEFAULT_FORMAT = "yyyy-MM-dd";

    private String format = DEFAULT_FORMAT;
    
    public void setFormat(String format) {
        this.format = format;
    }
    
    public void setAsText(String text) {
    	
    	SimpleDateFormat sdf = new SimpleDateFormat(this.format);
    	
        try {
			setValue(sdf.parse(text));
		}
        catch (ParseException e) {
        	throw new IllegalArgumentException("Could not convert from " + 
        			text + " to a java.util.Date using format '" + this.format, e);
		}
    }

}
